package com.andatsoft.laisim.adapter;

import java.util.List;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.andatsoft.laisim.R;
import com.andatsoft.laisim.entity.Song;
import com.andatsoft.laisim.theme.Theme;
import com.andatsoft.laisim.theme.ThemeManager;

public class CreatePlaylistDialogSongListAdapter extends ArrayAdapter<Song> {

	private List<Song> mSongList;
	private List<Integer> mSongListSelected;
	private LayoutInflater inflater;
	private Theme mTheme;

	public CreatePlaylistDialogSongListAdapter(Context context,
			List<Song> songs, List<Integer> songSelected) {
		super(context, 0, songs);
		this.mSongList = songs;
		this.mSongListSelected = songSelected;
		this.inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		mTheme = ThemeManager.getSelectedTheme();
	}

	public class ViewHolder {
		public TextView tvTitle;
		public TextView tvArtist;
	}

	@Override
	public int getCount() {
		if (mSongList == null)
			return 0;
		return mSongList.size();
	}

	@SuppressLint("NewApi")
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View view = convertView;
		ViewHolder vHolder;
		if (view == null) {
			view = inflater.inflate(R.layout.pager_main_song_list_item, parent,
					false);
			vHolder = new ViewHolder();
			TextView tvTitle = (TextView) view
					.findViewById(R.id.tv_pager_main_song_list_item_title);
			vHolder.tvTitle = tvTitle;
			TextView tvArtist = (TextView) view
					.findViewById(R.id.tv_pager_main_song_list_item_artist);
			vHolder.tvArtist = tvArtist;
			view.setTag(vHolder);
		} else {
			vHolder = (ViewHolder) view.getTag();
		}
		// Because the view holder is holding the reference of the text
		// view...
		vHolder.tvTitle.setText(mSongList.get(position).getTitle());
		vHolder.tvArtist.setText(mSongList.get(position).getArtist());
		// apply theme
		// // highlight view selected
		if (isSongSelected(position)) {

			vHolder.tvTitle.setTextSize(mTheme.getMPLSelectedTitleSize());
			vHolder.tvTitle.setTextColor(mTheme.getMPLSelectedTitleColor());
			vHolder.tvArtist.setTextColor(mTheme.getMPLSelectedArtistColor());
			vHolder.tvArtist.setTextSize(mTheme.getMPLSelectedArtistSize());
		} else {

			vHolder.tvTitle.setTextSize(mTheme.getMPLNormalTitleSize());
			vHolder.tvTitle.setTextColor(mTheme.getMPLNormalTitleColor());
			vHolder.tvArtist.setTextColor(mTheme.getMPLNormalArtistColor());
			vHolder.tvArtist.setTextSize(mTheme.getMPLNormalArtistSize());

		}
		return view;
	}

	public void setSongSelected(List<Integer> items) {
		mSongListSelected = items;
		notifyDataSetChanged();
	}

	private boolean isSongSelected(int pos) {
		if (mSongListSelected.contains(pos))
			return true;
		return false;
	}
}
